package servicedevmap

import "emsctrl/public"

var (
	BMS_SOC               = "soc"
	BMS_FAULT             = "Fault"
	BMS_ALARM             = "Alarm"
	BMS_COMMU_STATE       = "commuState"
	BMS_HIGHTVOLSTATE     = "HightVolState"
	BMS_ALARM_LEVEL       = "AlarmLevel"
	BMS_ONE_LEVEL_ALARM   = "OneLevelAlarm"
	BMS_TOW_LEVEL_ALARM   = "TowLevelAlarm"
	BMS_THREE_LEVEL_ALARM = "ThreeLevelAlarm"
	BMS_CELL_HIGHT_VOL    = "CellHightVol"
	BMS_CELL_LOW_VOL      = "CellLowVol"
	BMS_CELL_HIGHT_TEMP   = "CellHightTemp"
	BMS_CELL_LOW_TEMP     = "CellLowTemp"
)

const (
	BMS_ALARM_NO = iota
	BMS_ALARM_LEVEL_1
	BMS_ALARM_LEVEL_2
	BMS_ALARM_LEVEL_3
)

type bmsdevMap struct {
	bmsnumber       int32
	soc             int32
	fault           int
	alarm           int
	commu_state     int
	alarm_level     float32
	cell_hight_vol  float32
	cell_low_vol    float32
	cell_hight_temp float32
	cell_low_temp   float32
}

var bmsMap map[string]map[string]string

func init() {
	bmsMap = make(map[string]map[string]string)
}

func (b *bmsdevMap) Analysis_string(info []string) {
	var ytype string
	var key string
	var mapv string
	for i, s := range info {
		switch i {
		case 0:
			break
		case 1:
			ytype = s
		case 2:
			key = s
		case 3:
			mapv = s
		}
	}

	if _, ok := bmsMap[ytype]; ok {

	} else {
		bmsMap[ytype] = make(map[string]string)
	}
	bmsMap[ytype][key] = mapv
}

func (b *bmsdevMap) upGradeDevData(index int, devtype, devname, paratype string, datum public.DataVal) {
	reg := StationMap[index][devtype][devname]

	if _, ok := bmsMap[paratype]; ok {
		switch bmsMap[paratype][datum.Key] {
		case BMS_SOC,
			BMS_FAULT,
			BMS_COMMU_STATE,
			BMS_HIGHTVOLSTATE,
			BMS_CELL_HIGHT_VOL,
			BMS_CELL_LOW_VOL,
			BMS_CELL_HIGHT_TEMP,
			BMS_CELL_LOW_TEMP:
			if reg.RegMap == nil {
				reg.RegMap = make(map[string]RegInfo)
			} else {

			}
			reg.RegMap[bmsMap[paratype][datum.Key]] = RegInfo{Key: datum.Key, Val: datum.Val, Addr: datum.RegAddr, PType: paratype}
		case BMS_ONE_LEVEL_ALARM,
			BMS_TOW_LEVEL_ALARM,
			BMS_THREE_LEVEL_ALARM:
			if reg.RegMap == nil {
				reg.RegMap = make(map[string]RegInfo)
			} else {

			}
			reg.RegMap[bmsMap[paratype][datum.Key]] = RegInfo{Key: datum.Key, Val: datum.Val, Addr: datum.RegAddr, PType: paratype}
			info := RegInfo{}
			if reg.RegMap[BMS_THREE_LEVEL_ALARM].Val == 1 {
				info.Val = BMS_ALARM_LEVEL_3
			} else if reg.RegMap[BMS_TOW_LEVEL_ALARM].Val == 1 {
				info.Val = BMS_ALARM_LEVEL_2
			} else if reg.RegMap[BMS_ONE_LEVEL_ALARM].Val == 1 {
				info.Val = BMS_ALARM_LEVEL_1
			} else {
				info.Val = BMS_ALARM_NO
			}
			reg.RegMap[BMS_ALARM_LEVEL] = info
			if reg.RegMap[BMS_ALARM_LEVEL].Val == BMS_ALARM_NO {
				info.Val = 0

			} else {
				info.Val = 1
			}
			reg.RegMap[BMS_ALARM] = info
			break
		default:
		}
	}
	StationMap[index][devtype][devname] = reg
}

func (b *bmsdevMap) upGradeCurrPolicyPara(devtype, devName, paratype string) {
	*b = bmsdevMap{}
	for _, m := range StationMap {
		if _, ok := m[devtype][devName]; ok {
			reg := m[devtype][devName]
			b.bmsnumber++
			for s, info := range reg.RegMap {
				switch s {
				case BMS_SOC:
					b.soc += int32(info.Val)
				case BMS_FAULT:
					b.fault |= int(info.Val)
				case BMS_ALARM:
					b.alarm |= int(info.Val)
				case BMS_COMMU_STATE:
					b.commu_state |= int(info.Val)
				case BMS_HIGHTVOLSTATE:
				case BMS_ALARM_LEVEL:
					if b.alarm_level < info.Val {
						b.alarm_level = info.Val
					}
				case BMS_CELL_HIGHT_VOL:
					if b.cell_hight_vol < info.Val {
						b.cell_hight_vol = info.Val
					}
				case BMS_CELL_LOW_VOL:
					if b.cell_low_temp > info.Val {
						b.cell_low_temp = info.Val
					}
				case BMS_CELL_HIGHT_TEMP:
					if b.cell_hight_temp < info.Val {
						b.cell_hight_temp = info.Val
					}
				case BMS_CELL_LOW_TEMP:
					if b.cell_low_temp > info.Val {
						b.cell_low_temp = info.Val
					}
				}
			}
		}
	}
	b.soc = b.soc / b.bmsnumber
	G_curr_policy.RemoteMetering.Soc = b.soc
	G_curr_policy.RemoteMetering.BmsAlarmLevel = int(b.alarm_level)
	G_curr_policy.RemoteSignal.BMSFaultState = b.fault
	G_curr_policy.RemoteSignal.BMSAlarmState = b.alarm
	G_curr_policy.RemoteSignal.BmsUpVolState = int(b.cell_hight_vol)
}
